package edu.unh.sustain;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class NewsActivity extends ListActivity {

    private static final String TAG = "NewsActivity";

    private NewsFeedParser      feedParser;
    private List<Article>       articles;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_list);

        fillData();
    }

    private void getArticles() {
        feedParser = new NewsFeedParser(Constants.NEWS_FEED);
        articles = feedParser.parse();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(articles
                .get(position).getLink().toString()));
        startActivity(myIntent);
    }

    private void fillData() {
        getArticles();
        List<String> titles = new ArrayList<String>();
        for (Article article : articles) {
            titles.add(article.getTitle());
        }

        ArrayAdapter<String> titlesAdp = new ArrayAdapter<String>(this,
                R.layout.news_row, titles);
        setListAdapter(titlesAdp);
    }

}
